package dao;

import domain.Topic;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import java.util.List;

public class TopicDaoImpl implements TopicDao{
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public List<Topic> getAllTopic() {
        String sql = "select * from topic";
        List<Topic> topicList = template.query(sql, new BeanPropertyRowMapper<Topic>(Topic.class));
        return topicList;
    }

    public int deleteTopicByTid(int tid) {
        String sql = "delete from topic where tid = ?";
        return template.update(sql,tid);
    }

    public int modifyTopicByTid(int tid,String tname) {
        String sql = "update topic set tname = ? where tid = ?";
        return template.update(sql,tname,tid);
    }

    public int addTopic(String tname) {
        String sql = "insert into topic(tname) values(?)";
        return template.update(sql,tname);
    }

    public int countTopic() {
        String sql = "select count(*) from topic";
        return template.queryForObject(sql,Integer.class);
    }
}
